package com.huawei.spare.part.replace.framework;

import com.google.common.collect.Lists;
import com.huawei.spare.part.replace.models.Replacement;
import com.huawei.spare.part.replace.models.Uplink;
import com.huawei.spare.part.replace.utils.JsonMapper;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

public abstract class SmartPresenter implements Initializable {

  protected ResourceBundle bundle;

  @Override
  public void initialize(URL url, ResourceBundle rb) {
    this.bundle = rb;
    this.initialize();
  }

  public abstract void initialize();

  /**
   * get i18n string by key
   */
  public String getString(String key) {
    final ResourceBundle bundle = SmartDataBus.get().getBundle();
    if (bundle.containsKey(key)) {
      return bundle.getString(key);
    }
    return key;
  }

  /**
   * get i18n string list by keys
   */
  public List<String> getString(String... keys) {
    List<String> result = Lists.newArrayList();
    for (String key : keys) {
      result.add(getString(key));
    }
    return result;
  }

  protected void initDebugReplacement() {
    if (SmartDataBus.get().replacement().getProject() == null
        && SmartDataBus.get().getConfigs().getDebug()) {
      // debug purpose
      final InputStream is = SmartPresenter.class.getResourceAsStream("/replacement.json");
      final Replacement replacement = JsonMapper.nonDefaultMapper().getBean(is, Replacement.class);
      replacement.setUplinks(Lists.newArrayList(
          new Uplink("port1", "port1", "this is a very long remark"),
          new Uplink("port2", "port2", "this is a very long remark"),
          new Uplink("port3", "port3", "this is a very long remark"),
          new Uplink("port4", "port4", "this is a very long remark"),
          new Uplink("port5", "port5",
              "this is a very long remark, this is a very long remark, this is a very long remark, this is a very long remark"),
          new Uplink("port1", "port1", "this is a very long remark"),
          new Uplink("port2", "port2", "this is a very long remark"),
          new Uplink("port3", "port3", "this is a very long remark"),
          new Uplink("port4", "port4", "this is a very long remark"),
          new Uplink("port5", "port5",
              "this is a very long remark, this is a very long remark, this is a very long remark, this is a very long remark")
      ));

      SmartDataBus.get().setReplacement(replacement);
    }
  }
}
